package service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dao.OrderDao;
import pojo.Order;
import pojo.OrderItem;
import service.OrderService;
@Service
@Transactional
@Configuration
@PropertySource("classpath:default.properties")
public class OrderServiceImpl implements OrderService {

	@Resource(name="orderDaoImpl")
	private OrderDao orderDao;
	
    @Resource
    Environment env;

	
	public List<Order> findOrderByStatusByTime(String status, Date date){
		return orderDao.findOrderByStatusByTime(status,date);
	}
	
	@Override
	public List<Order> autoConfirm() {
//		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		Date now = dateFormat.
		Date now = new Date();
		now.setHours(now.getMinutes()-Integer.parseInt(env.getProperty("auto.confirm.time")));
		List<Order> orders = orderDao.findOrderByStatusByTime("DELIVER",now );
		for (Order order : orders) {
			System.out.println(order.getId());
			order.setStatus("SUCCESS");
		}
		return orders;
	}
	
	@Override
	public List<Order> findOrderByStatus(String status,String mId) {
		if("ALL".equals(status)){
			return orderDao.findAll(mId);
		}
		else{
			return orderDao.findOrderByStatus(status,mId);
		}
	}

	@Override
	public Order updateOrderStatus(String id,String status,String mId) {
		return orderDao.updateOrderStatus(id,status,mId);
	}

	@Override
	public List<OrderItem> findOrderItem(String id) {
		return orderDao.findOrderItem(id);
	}

	@Override
	public List<Order> findAll(String mId) {
		return orderDao.findAll(mId);
	}

	@Override
	public Order findByprocessNumber(String processNumber) {
		return orderDao.findByprocessNumber(processNumber);
	}

	@Override
	public List<Order> findByMerchantId(String mId) {
		List<Order> orders =  orderDao.findOrderByStatus("SUCCESS", mId);
		List<Order> backOrders = new ArrayList<Order>();
		for(Order o: orders){
			if(o.getComment().size() >= 1)
				backOrders.add(o);
		}
		return backOrders;
	}


	
	
}
